package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveUserLike(Long userId, Long likeUserId) {

        // 1、先查询当前用户是否曾经喜欢过对方
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        if (!mongoTemplate.exists(query, UserLike.class)) {
            // 2、如果没查到，就添加喜欢记录
            UserLike userLike = new UserLike();
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            mongoTemplate.save(userLike);
        }

    }

    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        // 条件一：你喜欢对方
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        // 条件二：对方喜欢你
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        // 条件拼接
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        Query query = new Query(criteria);
        return mongoTemplate.count(query, UserLike.class) == 2;
    }

    @Override
    public void removeUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    @Override
    public Long findEachLoveCount(Long userId) {
        // 查询好友表
        Query query = Query.query(Criteria.where("userId").is(userId));

        return mongoTemplate.count(query, Friend.class);
    }

    @Override
    public Long findloveCount(Long userId) {
        // 查询喜欢表
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long findFanCount(Long userId) {
        // 查询喜欢表
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public PageBeanVo findEachLoveByPage(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
        // 2、查询好友
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (Friend friend : friendList) {
            RecommendUser user = changeRecommendUser(friend.getUserId(), friend.getFriendId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, Friend.class);
        // 4、返回分页对象
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    @Override
    public PageBeanVo findloveByPage(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
        // 2、查询你喜欢的
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            RecommendUser user = changeRecommendUser(userLike.getUserId(), userLike.getLikeUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, UserLike.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    @Override
    public PageBeanVo findFanByPage(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("likeUserId").is(userId)).skip(index).limit(pageSize);
        // 2、查询你喜欢的
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            RecommendUser user = changeRecommendUser(userLike.getLikeUserId(), userLike.getUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, UserLike.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    @Override
    public PageBeanVo findVisitorByPage(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
        // 2、查询历史访客
        List<Visitors> visitorsList = mongoTemplate.find(query, Visitors.class);
        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (Visitors visitors : visitorsList) {
            RecommendUser user = changeRecommendUser(visitors.getUserId(), visitors.getVisitorUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, Visitors.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    // 抽取一个方法，转为recommendUser
    private RecommendUser changeRecommendUser(Long toUserId, Long userId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) { // 构建默认数据
            recommendUser = new RecommendUser();
            recommendUser.setToUserId(toUserId);
            recommendUser.setUserId(userId);
            recommendUser.setScore(95D);
        }

        return recommendUser;
    }
}
